'use strict';


angular.
module('sfhtHaitaoCmsApp').
controller('ModuleHtmlCtrl', function($scope, $route, $routeParams, $injector, $modal) {
    var commonBizService = $injector.get("CommonBiz");

    /**
     * 查询模块数据列表
     * @param  {int} 页面id
     * @param  {string} 模块名称
     * @param  {function} 后续处理函数
     * @return {array}
     */
    function getDataList(pageid, placeholder) {
        commonBizService.getDataList(pageid, placeholder)
            .then(function(data) {
                $scope.mainproducts = data.value[0];
                if ($scope.mainproducts.cmsData) {
                    $scope.isAdd = false;
                    _.each($scope.mainproducts.cmsData, function(cmsData, ckey, list) {
                        _.each(cmsData.properties, function(property, key, list) {

                            if (property.field.fieldType == 'Date') {
                                var date = parseInt($scope.mainproducts.cmsData[ckey].properties[key].propertyValue)
                                $scope.mainproducts.cmsData[ckey].properties[key].propertyValue = new Date(date)
                            }
                        });
                    });
                } else {
                    $scope.isAdd = true;
                    commonBizService.getFieldOfSubModule($scope.mainproducts.submoduleId)
                        .then(function(data) {
                            $scope.item = data.value[0]
                        });
                }
            }, function() {

            });
    }

    /**
     * 添加模块数据
     * @param  {object} 数据对象
     * @return {array}
     */
    $scope.add = function(item, $event) {
        $event.preventDefault();
        var dataList = [{
            listId: $scope.mainproducts.listId,
            properties: [{
                'propertyName': 'html',
                'propertyValue': item.propertyValue
            }]
        }];

        commonBizService.addCmsData(dataList)
            .then(function(data) {
                $route.reload();
                // window.location.reload(true);
            }, function(data) {
                alert("新增失败！！");
            })
    };

    /**
     * 查询模块数据的详细列表
     * @param  {function} 后续处理函数
     * @return {array}
     */
    var getModuleDetail = function(fn) {
        commonBizService.getModuleDetail($routeParams.moduleName, $routeParams.pageType, false)
            .then(function(data) {
                $scope.module = data;
                fn();
            });
    }

    $scope.init = function() {
        $scope.pageId = $routeParams.pageid;
        $scope.pageType = $routeParams.pageType;
        $scope.placeholder = $routeParams.placeholder;
        getModuleDetail(function() {
            getDataList($routeParams.pageid, $routeParams.placeholder);
        });
    };

    $scope.loaded = function(_editor) {};

    $scope.changed = function(e) {};

    /**
     * 修改模块数据
     * @param  {object} 数据对象
     * @return {array}
     */
    $scope.save = function(param, $event) {
        commonBizService.updateSingleCmsData(param)
            .then(function(data) {
                $route.reload();
                // window.location.reload();
            }, function(data) {
                alert("数据修改失败，错误码为：" + data);
            })
    };
    /**
     * 发布数据
     */
    $scope.publishData = function() {
        if (window.confirm("确认发布?")) {
            commonBizService.publishData($scope.pageId, $scope.placeholder)
                .then(function(data) {
                    $route.reload();
                    // window.location.reload(true);
                }, function(data) {
                    alert("发布失败!" + data);
                });;
        }
    };
    /**
     * 还原数据
     */
    $scope.revert = function() {
        if (window.confirm("还原至上一个发布版本?")) {
            commonBizService.revert($scope.pageId, $scope.placeholder)
                .then(function(data) {
                    $route.reload();
                    // window.location.reload(true);
                }, function(data) {
                    alert("还原失败!" + data);
                });;
        }
    };
    /**
     *打开专题页地址
     **/
    $scope.viewOnline = function() {
        commonBizService.getAccessURL($scope.pageId)
            .then(
                function(data) {
                    window.open(data.value);
                }
            );
    };
});
